package com.cloud.drore.eboss.sso.service.impl;

import com.cloud.drore.eboss.sso.base.page.Pagination;
import com.cloud.drore.eboss.sso.base.page.PaginationResult;
import com.cloud.drore.eboss.sso.base.page.SpecificationUtil;
import com.cloud.drore.eboss.sso.dao.FunctionDao;
import com.cloud.drore.eboss.sso.entity.Function;
import com.cloud.drore.eboss.sso.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class FunctionServiceImpl extends EntityServiceImpl<Function, Long, FunctionDao> implements FunctionService {

	@Override
	public List<Function> findList(){

		return entityDao.findList();
	}

	@Override
	public Function findById(Long id){

		return entityDao.findOne(id);
	}


	@Override
	public Function findByRightCode(String permissionName){
		return entityDao.findByRightCode(permissionName);
	}


	@Override
	public Function findByPid(Integer pid){
		return entityDao.findByPid(pid);
	}

	@Override
	public List<Function> findListByPid(Long pid){

		return entityDao.findListByPid(pid);
	}

	@Override
	public List<Function> findByTenantId(String tenantid) {
		return entityDao.findByTenantId(tenantid);
	}


	@Override
	public List<Function> findAllByPage(String projectname){

		List<Function> functions = entityDao.findListByTenantId(projectname);

		List<Function> parent = functions.stream().filter(f->f.getPid()==0).collect(Collectors.toList());

		for(Function p:parent){
			for(Function c:functions){
				if(c.getPid() == p.getId())
					p.getChildren().add(c);
			}

		}
		return parent;

	}

	@Override
	@Autowired
	public void setEntityDao(FunctionDao entityDao) {
		this.entityDao = entityDao;
	}
}